<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

require_once(dirname(__FILE__) . '/lib_erp_supplier.php');

/*
*是否是管辖的产品(在订单编辑页面，需要判断某个产品是否属于某个采购员所管)
*/
function is_admin_goods($admin_id,$goods_id='',$goods_sn='')
{
	$supplier_id_array=array();
	
	$supplier_info=get_admin_supplier($admin_id);
		
	if(!empty($supplier_info))
	{
		foreach($supplier_info as $key => $item)
		{
			array_push($supplier_id_array,$item['supplier_id']);
		}
	}
	
	if(!empty($goods_id) && empty($goods_sn))
	{
		$sql="select suppliers_id from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
	}
	elseif(empty($goods_id) && !empty($goods_sn))
	{
		$sql="select suppliers_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
	}
	else{
		return false;
	}
	
	$suppliers_id=$GLOBALS['db']->getOne($sql);
	
	if(!empty($suppliers_id) && in_array($suppliers_id,$supplier_id_array))
	{
		return true;
	}
	else{
		return false;
	}
}

/*
*是否是某个供应商供应的商品
*/
function is_supplier_goods($supplier_id,$goods_id='',$goods_sn='')
{
	if(empty($supplier_id))
	{
		return false;
	}
	if(!empty($goods_id) && empty($goods_sn))
	{
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."' and suppliers_id='".$supplier_id."'";
	}
	elseif(empty($goods_id) && !empty($goods_sn))
	{
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."' and suppliers_id='".$supplier_id."'";
	}
	else{
		return false;
	}
	$num=$GLOBALS['db']->getOne($sql);
	if($num==0)
	{
		return false;
	}
	else{
		return true;
	}
}

/*
function get_goods_info($goods_id='',$goods_sn='')
{
	if(!empty($goods_id) && empty($goods_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
	}
	elseif(empty($goods_id) && !empty($goods_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
	}	
	else{
		return false;
	}

	return $GLOBALS['db']->getRow($sql);
}
*/

/**
 * 判断产品是否存在，是否已被删除
 */
function is_goods_exist($goods_id='',$goods_sn='')
{
	if(!empty($goods_id) && empty($goods_sn))
	{
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
	}
	elseif(empty($goods_id) && !empty($goods_sn))
	{
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
	}	
	else{
		return false;
	}

	$num=$GLOBALS['db']->getOne($sql);
		
	if($num==0)
	{
		return false;
	}
	else
	{
		return true;
	}			
}


/*
*获取某个产品的库存数
*/
function get_goods_stock($warehouse_id,$goods_id,$attr_id)
{	
	if(!empty($attr_id))
	{
		$w_qty=0;
		$d_qty=0;
		
		//获取入库总数
		$sql="select qty from ".$GLOBALS['ecs']->table('erp_stock')." where goods_id='".$goods_id."' and warehouse_id='".$warehouse_id."' and goods_attr_id='".$attr_id."' and stock_style='w'";
		$result=$GLOBALS['db']->getAll($sql);
		
		if(!empty($result))
		{
			foreach($result as $item)
			{
				$w_qty+=$item['qty'];
			}
		}
		
		//获取出库总数
		$sql="select qty from ".$GLOBALS['ecs']->table('erp_stock')." where goods_id='".$goods_id."' and warehouse_id='".$warehouse_id."' and goods_attr_id='".$attr_id."' and stock_style='d'";
		
		$result=$GLOBALS['db']->getAll($sql);
		
		if(!empty($result))
		{
			foreach($result as $item)
			{
				$d_qty+=$item['qty'];
			}
		}	
		
		return $w_qty+$d_qty; 
	}
	else{
		$w_qty=0;
		$d_qty=0;
		
		//获取入库总数
		$sql="select qty from ".$GLOBALS['ecs']->table('erp_stock')." where goods_id='".$goods_id."' and warehouse_id='".$warehouse_id."'  and stock_style='w'";
		$result=$GLOBALS['db']->getAll($sql);
		
		if(!empty($result))
		{
			foreach($result as $item)
			{
				$w_qty+=$item['qty'];
			}
		}
		
		//获取出库总数
		$sql="select qty from ".$GLOBALS['ecs']->table('erp_stock')." where goods_id='".$goods_id."' and warehouse_id='".$warehouse_id."'  and stock_style='d'";
		
		$result=$GLOBALS['db']->getAll($sql);
		
		if(!empty($result))
		{
			foreach($result as $item)
			{
				$d_qty+=$item['qty'];
			}
		}	
		
		return $w_qty+$d_qty; 		
	}

}


?>